package com.salt.modules.sys.utils;

import com.salt.common.utils.SpringContextHolder;
import com.salt.modules.sys.entity.Admin;
import com.salt.modules.sys.entity.User;
import com.salt.modules.sys.mapper.AdminMapper;

import java.util.List;

/**
 * @Description
 * @Author salt
 * @Date 2022/1/17
 * @Version 1.0.1
 */
public class AdminUtils {

    private static AdminMapper adminMapper = SpringContextHolder.getBean(AdminMapper.class);

    public static List<Admin> findAdminList(User user) {
        return adminMapper.findList(new Admin(user));
    }

    /**
     * 判断某个用户是否为某个系统的管理员
     * @param user 用户
     * @return 布尔值
     */
    public static Boolean isAdmin(User user, String tenant) {
        List<Admin> adminList = findAdminList(user);
        if (adminList != null && !adminList.isEmpty()) {
            for (Admin admin:adminList) {
                if (admin.getUser().getId().equals(user.getId()) &&
                        admin.getTenant().equals(tenant))
                    return true;
            }
        }
        return false;
    }

    /**
     * 判断某个用户是否为某个系统的管理员
     * @return 布尔值
     */
    public static Boolean isAdmin(String tenant) {
        User user = UserUtils.getUser();
        return isAdmin(user, tenant);
    }

    /**
     * 判断某个用户是否为管理员
     * @return
     */
    public static Boolean isAdmin() {
        User user = UserUtils.getUser();
        List<Admin> adminList = findAdminList(user);
        if (adminList != null && !adminList.isEmpty())
            return true;
        return false;
    }
}
